/**
 * Copyright (c) 2006 eMundo GmbH. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 *
 * Project: agil-client-web
 * File: GwtClientUI.java
 *
 */
package eu.emundo.agilpro.web.client;

import java.util.HashMap;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import eu.emundo.agilpro.web.client.dto.GwtParameterDTO;
import eu.emundo.agilpro.web.client.generic.GwtGenericParameterPanel;
import eu.emundo.agilpro.web.client.generic.GwtGenericUI;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 * 
 * @author Fabian Wohlschl\u00E4ger
 */
public class GwtClientUI implements EntryPoint {
	/**
	 * Remote service
	 */
	private GwtRemoteServiceAsync mDataService;

	/**
	 * For i18n
	 */
	private GwtClientConstants mConstants = (GwtClientConstants) GWT.create(GwtClientConstants.class);

	/**
	 * The menu
	 */
	private HorizontalPanel mMenu;

	/**
	 * Toggle-button for controlling the visability of the sidebar
	 */
	private Button mSidebarToogleButton;

	/**
	 * Performing next step
	 */
	private Button mNextButton;

	/**
	 * Performing pause
	 */
	private Button mDelayButton;

	/**
	 * Performing a cancel
	 */
	private Button mCancelButton;

	/**
	 * Viewing status
	 */
	private HorizontalPanel mStatusPanel;

	/**
	 * Current activity panel
	 */
	private HorizontalPanel mCurrentActivityPanel;

	/**
	 * Current activity label
	 */
	private Label mCurrentActivityLabel;

	/**
	 * Current activity
	 */
	private Label mCurrentActivity;

	/**
	 * Current action panel
	 */
	private HorizontalPanel mCurrentActionPanel;

	/**
	 * Current action label
	 */
	private Label mCurrentActionLabel;

	/**
	 * Current action
	 */
	private Label mCurrentAction;

	/**
	 * Current role panel
	 */
	private HorizontalPanel mCurrentRolePanel;

	/**
	 * Current role label
	 */
	private Label mCurrentRoleLabel;

	/**
	 * Current role
	 */
	private Label mCurrentRole;

	/**
	 * Viewing the logo
	 */
	private HorizontalPanel mIconPanel;

	/**
	 * Logo
	 */
	private Image mLogo;

	/**
	 * Contains mSidebar and mFrame
	 */
	private HorizontalPanel mContent;

	/**
	 * Sidebar
	 */
	private VerticalPanel mSidebar;

	/**
	 * Contains the widgets for selecting a role
	 */
	private VerticalPanel mRolePanel;

	/**
	 * Headline
	 */
	private Label mRoleLabel;

	/**
	 * Displaying possible roles
	 */
	private ListBox mRoleListBox;

	/**
	 * Contains the widgets for selecting a activity
	 */
	private VerticalPanel mActivityPanel;

	/**
	 * Headline
	 */
	private Label mActivityLabel;

	/**
	 * Displaying possible activites
	 */
	private ListBox mActivityListBox;

	/**
	 * Contains the widgets for selecting a deferred activity
	 */
	private VerticalPanel mDelayedActivityPanel;

	/**
	 * Headline
	 */
	private Label mDelayedActivityLabel;

	/**
	 * Displaying deferred activites
	 */
	private ListBox mDelayedActivityListBox;

	/**
	 * Contains the widgets for working with parameters
	 */
	private VerticalPanel mParameterPanel;

	/**
	 * Headline
	 */
	private Label mParameterLabel;

	/**
	 * Displays the parameters
	 */
	private GwtGenericParameterPanel mGenericParameterPanel;

	/**
	 * Displays the websites
	 */
	private Frame mFrame;

	/**
	 * GenericUI
	 */
	private GwtGenericUI mGenericUI;

	/**
	 * Is true if an activity is started
	 */
	private boolean mIsActivityStartedFlag = false;

	/**
	 * Is true if a generic-ui was displayed
	 */
	private boolean mWasGenericUIFlag = false;

	/**
	 * Is true if a generic-parameter-panel was displayed
	 */
	private boolean mWasGenericParameterPanelFlag = false;

	/**
	 * Listener for starting activities
	 * 
	 * @author Fabe
	 */
	private class NextButtonListener implements ClickListener {
		/**
		 * Method is called when a click-event occurs
		 * 
		 * @param pWidget
		 *            which throws event
		 * 
		 * (non-Javadoc)
		 * @see com.google.gwt.user.client.ui.ClickListener#onClick(com.google.gwt.user.client.ui.Widget)
		 */
		public void onClick(Widget pWidget) {
			nextAction();
		}
	}

	/**
	 * Listener for delaying actions
	 * 
	 * @author Fabe
	 * 
	 */
	private class DelayButtonListener implements ClickListener {
		/**
		 * Method is called when a click-event occurs
		 * 
		 * @param pWidget
		 *            which throws event
		 * 
		 * (non-Javadoc)
		 * @see com.google.gwt.user.client.ui.ClickListener#onClick(com.google.gwt.user.client.ui.Widget)
		 */
		public void onClick(Widget pWidget) {
			delayAction();
		}
	}

	/**
	 * Listener for canceling actions
	 * 
	 * @author Fabe
	 * 
	 */
	private class CancelButtonListener implements ClickListener {
		/**
		 * Method is called when a click-event occurs
		 * 
		 * @param pWidget
		 *            which throws event
		 * 
		 * (non-Javadoc)
		 * @see com.google.gwt.user.client.ui.ClickListener#onClick(com.google.gwt.user.client.ui.Widget)
		 */
		public void onClick(Widget pWidget) {
			cancelAction();
		}
	}

	/**
	 * Listener for controlling the visability of the sidebar
	 * 
	 * @author Fabe
	 * 
	 */
	private class SidebarListener implements ClickListener {
		boolean lIsSidebarVisible = true;

		/**
		 * Method is called when a click-event occurs
		 * 
		 * @param pWidget
		 *            which throws event
		 * 
		 * (non-Javadoc)
		 * @see com.google.gwt.user.client.ui.ClickListener#onClick(com.google.gwt.user.client.ui.Widget)
		 */
		public void onClick(Widget pWidget) {
			if (lIsSidebarVisible) {
				lIsSidebarVisible = false;
				mSidebar.setVisible(false);
				mSidebarToogleButton.setText(mConstants.displaySidebar());
			} else {
				lIsSidebarVisible = true;
				mSidebar.setVisible(true);
				mSidebarToogleButton.setText(mConstants.hideSidebar());
			}
		}
	}

	/**
	 * Listener for controlling the change of the role
	 * 
	 * @author Fabe
	 * 
	 */
	private class RoleListBoxListener implements ChangeListener {
		/*
		 * Method is called when a change-event occurs
		 * 
		 * @param pWidget which throws event
		 * 
		 * (non-Javadoc)
		 * 
		 * @see com.google.gwt.user.client.ui.ChangeListener#onChange(com.google.gwt.user.client.ui.Widget)
		 */
		public void onChange(Widget pWidget) {
			final String lNewRole = mRoleListBox.getItemText(mRoleListBox.getSelectedIndex());

			// Setting role
			AsyncCallback lRoleCallback = new AsyncCallback() {
				public void onSuccess(Object pResult) {
					refreshActivityListBox(lNewRole);

					refreshDelayedActivityListBox(lNewRole);
				}

				public void onFailure(Throwable pCaught) {
					Window.alert(mConstants.asynchronousCommunicationError() + "\n" + pCaught.getLocalizedMessage());
				}
			};
			mDataService.changedRole(lNewRole, lRoleCallback);
		}
	}

	/**
	 * Listener for controlling the change of the activity
	 * 
	 * @author Fabe
	 * 
	 */
	private class ActivityListBoxListener implements ChangeListener {
		/*
		 * Start an activity
		 * 
		 * (non-Javadoc)
		 * 
		 * @see com.google.gwt.user.client.ui.ChangeListener#onChange(com.google.gwt.user.client.ui.Widget)
		 */
		public void onChange(Widget pWidget) {
			// If one activity is already startet cancel it
			cancelAction();

			String lCurrentActivity = mActivityListBox.getItemText(mActivityListBox.getSelectedIndex());
			refreshActivityListBox(mRoleListBox.getItemText(mRoleListBox.getSelectedIndex()));

			AsyncCallback lStartActivityCallback = new AsyncCallback() {
				public void onSuccess(Object pResult) {
					mIsActivityStartedFlag = true;

					refreshExecutionPanel();

					mNextButton.setEnabled(true);
					mDelayButton.setEnabled(true);
					mCancelButton.setEnabled(true);
				}

				public void onFailure(Throwable pCaught) {
					Window.alert(mConstants.asynchronousCommunicationError() + "\n" + pCaught.getLocalizedMessage());
				}
			};
			mDataService.startActivity(lCurrentActivity, lStartActivityCallback);
		}
	}

	/**
	 * Listener for controlling the change of the deffered activities
	 * 
	 * @author Fabe
	 * 
	 */
	private class DelayedActivityListBoxListener implements ChangeListener {
		/**
		 * Start an delayed activity
		 * 
		 * (non-Javadoc)
		 * 
		 * @see com.google.gwt.user.client.ui.ChangeListener#onChange(com.google.gwt.user.client.ui.Widget)
		 */
		public void onChange(Widget pWidget) {
			// If one activity is already startet cancel it
			cancelAction();

			String lCurrentDelayedActivity = mDelayedActivityListBox.getItemText(mDelayedActivityListBox
					.getSelectedIndex());

			AsyncCallback lStartDelayedActivityCallback = new AsyncCallback() {
				public void onSuccess(Object pResult) {
					refreshDelayedActivityListBox(mRoleListBox.getItemText(mRoleListBox.getSelectedIndex()));

					mIsActivityStartedFlag = true;
					mWasGenericUIFlag = false;
					mWasGenericParameterPanelFlag = false;

					mNextButton.setEnabled(true);
					mDelayButton.setEnabled(true);
					mCancelButton.setEnabled(true);

					refreshExecutionPanel();
				}

				public void onFailure(Throwable pCaught) {
					Window.alert(mConstants.asynchronousCommunicationError() + "\n" + pCaught.getLocalizedMessage());
				}
			};
			mDataService.resumeDelayedActivity(lCurrentDelayedActivity, lStartDelayedActivityCallback);
		}
	}

	/**
	 * Performing nextAction and setting URL in mFrame
	 * 
	 */
	private void nextAction() {
		mNextButton.setEnabled(false);

		final AsyncCallback lNextActionCallback = new AsyncCallback() {
			public void onSuccess(Object pResult) {
				refreshExecutionPanel();
			}

			public void onFailure(Throwable pCaught) {
				Window.alert(mConstants.asynchronousCommunicationError() + "\n" + pCaught.getLocalizedMessage());
			}
		};

		/*
		 * If a GenericUI or GenericParameterPanel was displayed before the
		 * parameters should be sent
		 */
		if (mWasGenericUIFlag || mWasGenericParameterPanelFlag) {
			GwtParameterDTO lDto = new GwtParameterDTO();

			if (mWasGenericParameterPanelFlag) {
				mWasGenericParameterPanelFlag = false;
				lDto.setParameters(mGenericParameterPanel.getParameters());
			} else {
				mWasGenericUIFlag = false;
				lDto.setParameters(mGenericUI.getParameters());
			}

			// Set the parameters of the current action
			AsyncCallback lParameterCallback = new AsyncCallback() {
				public void onSuccess(Object pResult) {
					mDataService.nextAction(lNextActionCallback);
				}

				public void onFailure(Throwable pCaught) {
					Window.alert(mConstants.asynchronousCommunicationError() + "\n" + pCaught.getLocalizedMessage());
				}
			};
			mDataService.setParameters(lDto, lParameterCallback);
		} else {
			mDataService.nextAction(lNextActionCallback);
		}
	}

	/**
	 * Performing cancelAction
	 */
	private void cancelAction() {
		if (mIsActivityStartedFlag) {
			AsyncCallback lCancelActionCallback = new AsyncCallback() {
				public void onSuccess(Object pResult) {
					resetExecutionPanel();
				}

				public void onFailure(Throwable pCaught) {
					Window.alert(mConstants.asynchronousCommunicationError() + "\n" + pCaught.getLocalizedMessage());
					resetExecutionPanel();
				}
			};
			mDataService.cancelAction(lCancelActionCallback);
		}
	}

	private void resetExecutionPanel() {
		mNextButton.setEnabled(false);
		mDelayButton.setEnabled(false);
		mCancelButton.setEnabled(false);
		mGenericParameterPanel.setParameters(null);
		mGenericParameterPanel.setEnabled(false);
		mParameterPanel.setVisible(false);

		// Reset GUI
		mCurrentActivity.setText("");
		mCurrentAction.setText("");
		mCurrentRole.setText("");

		mGenericParameterPanel.removeFromParent();
		mGenericParameterPanel = new GwtGenericParameterPanel();
		mGenericParameterPanel.setPixelSize(175, 75);
		mParameterPanel.add(mGenericParameterPanel);

		mFrame.setUrl("");
		mFrame.setVisible(false);

		mGenericUI.setVisible(false);
	}

	/**
	 * Performing delayAction
	 */
	private void delayAction() {
		if (mIsActivityStartedFlag) {
			// Delay action
			AsyncCallback lDelayCallback = new AsyncCallback() {
				public void onSuccess(Object pAction) {
					mNextButton.setEnabled(false);
					mDelayButton.setEnabled(false);
					mCancelButton.setEnabled(false);
					mGenericParameterPanel.setEnabled(false);
					mIsActivityStartedFlag = false;
					resetExecutionPanel();

					refreshDelayedActivityListBox(mRoleListBox.getItemText(mRoleListBox.getSelectedIndex()));
				}

				public void onFailure(Throwable pCaught) {
					Window.alert(mConstants.asynchronousCommunicationError() + "\n" + pCaught.getLocalizedMessage());
				}
			};
			mDataService.delayAction(lDelayCallback);
		}
	}

	/**
	 * Refreshes the information in the mStatusPanel
	 * 
	 */
	private void refreshStatusPanel() {
		// Get and set current role
		AsyncCallback lCurrentRoleCallback = new AsyncCallback() {
			public void onSuccess(Object pRole) {
				mCurrentRole.setText((String) pRole);
			}

			public void onFailure(Throwable pCaught) {
				Window.alert(mConstants.asynchronousCommunicationError() + "\n" + pCaught.getLocalizedMessage());
			}
		};
		mDataService.getActiveRole(lCurrentRoleCallback);

		// Get and set current activity
		AsyncCallback lCurrentActivityCallback = new AsyncCallback() {
			public void onSuccess(Object pActivity) {
				mCurrentActivity.setText((String) pActivity);
			}

			public void onFailure(Throwable pCaught) {
				Window.alert(mConstants.asynchronousCommunicationError() + "\n" + pCaught.getLocalizedMessage());
			}
		};
		mDataService.getActiveActivity(lCurrentActivityCallback);

		// Get and set current action
		AsyncCallback lCurrentActionCallback = new AsyncCallback() {
			public void onSuccess(Object pAction) {
				mCurrentAction.setText((String) pAction);
			}

			public void onFailure(Throwable pCaught) {
				Window.alert(mConstants.asynchronousCommunicationError() + "\n" + pCaught.getLocalizedMessage());
			}
		};
		mDataService.getActiveAction(lCurrentActionCallback);
	}

	/**
	 * Refreshes the information in the mParameterTable
	 * 
	 * @param pIsEditable
	 *            true if the parameters should be editable
	 */
	private void refreshParameterPanel(final boolean pIsEditable) {
		// Get the parameters of the current action
		AsyncCallback lParameterCallback = new AsyncCallback() {
			public void onSuccess(Object pResult) {
				GwtParameterDTO lDTO = (GwtParameterDTO) pResult;
				HashMap lParams = lDTO.getParameters();

				// When no parameters are defined, the parameterlist should not
				// be displayed
				if (lParams.size() != 0) {
					mGenericParameterPanel.setParameters(lParams);
					mGenericParameterPanel.setEnabled(pIsEditable);
					mParameterPanel.setVisible(true);
				}
			}

			public void onFailure(Throwable pCaught) {
				Window.alert(mConstants.asynchronousCommunicationError() + "\n" + pCaught.getLocalizedMessage());
			}
		};
		mDataService.getParameters(lParameterCallback);
	}

	/**
	 * Set mFrame visible and set URL or generate GenericUI
	 */
	private void refreshExecutionPanel() {
		AsyncCallback lGetUrlCallback = new AsyncCallback() {
			public void onSuccess(Object pUrl) {
				final String lUrl = (String) pUrl;

				AsyncCallback lGetStopPollingFlagCallback = new AsyncCallback() {
					public void onSuccess(Object pStopPollingFlag) {
						if (!((Boolean) pStopPollingFlag).booleanValue()) {
							boolean lIsParameterPanelEditable = true;

							// Gui-Handling
							// If a GenericUI should be displayed
							if (lUrl == null) {
								lIsParameterPanelEditable = false;
								mWasGenericUIFlag = true;
								mWasGenericParameterPanelFlag = false;

								// Get the parameters of the current action
								AsyncCallback lParameterCallback = new AsyncCallback() {
									public void onSuccess(Object pResult) {
										GwtParameterDTO lDTO = (GwtParameterDTO) pResult;

										mFrame.setVisible(false);

										mGenericUI.setVisible(true);
										mGenericUI.setParameters(lDTO.getParameters());
									}

									public void onFailure(Throwable pCaught) {
										Window.alert(mConstants.asynchronousCommunicationError() + "\n"
												+ pCaught.getLocalizedMessage());
									}
								};
								mDataService.getVisibleParameters(lParameterCallback);
							} else {
								mWasGenericUIFlag = false;
								mWasGenericParameterPanelFlag = true;

								mGenericUI.setVisible(false);

								mFrame.setVisible(true);
								mFrame.setUrl(lUrl);
							}

							mNextButton.setEnabled(true);
							refreshStatusPanel();
							refreshParameterPanel(lIsParameterPanelEditable);
						}
					}

					public void onFailure(Throwable pCaught) {
						Window
								.alert(mConstants.asynchronousCommunicationError() + "\n"
										+ pCaught.getLocalizedMessage());
					}
				};
				mDataService.getStopPollingFlag(lGetStopPollingFlagCallback);
			}

			public void onFailure(Throwable pCaught) {
				Window.alert(mConstants.asynchronousCommunicationError() + "\n" + pCaught.getLocalizedMessage());
			}
		};
		mDataService.getUrl(lGetUrlCallback);
	}

	/**
	 * Refreshes mActivityListBox
	 */
	private void refreshActivityListBox(String pNewRole) {
		mActivityListBox.clear();

		AsyncCallback lActivitiesCallback = new AsyncCallback() {
			public void onSuccess(Object pResult) {
				// Insert activities
				String[] lActivities = (String[]) pResult;
				for (int lI = 0, lMaxI = lActivities.length; lI < lMaxI; lI++) {
					mActivityListBox.addItem(lActivities[lI]);
				}
			}

			public void onFailure(Throwable pCaught) {
				Window.alert(mConstants.asynchronousCommunicationError() + "\n" + pCaught.getLocalizedMessage());
			}
		};
		mDataService.getActivitiesForRole(pNewRole, lActivitiesCallback);
	}

	/**
	 * Refreshes mDelayedActivityListBox
	 */
	private void refreshDelayedActivityListBox(String pNewRole) {
		mDelayedActivityListBox.clear();

		// Insert delayed activities
		AsyncCallback lDelayedActivityCallback = new AsyncCallback() {
			public void onSuccess(Object pResult) {
				// Insert delayed activities
				String[] lDelayedActivities = (String[]) pResult;
				for (int lI = 0, lMaxI = lDelayedActivities.length; lI < lMaxI; lI++) {
					mDelayedActivityListBox.addItem(lDelayedActivities[lI]);
				}
			}

			public void onFailure(Throwable pCaught) {
				Window.alert(mConstants.asynchronousCommunicationError() + "\n" + pCaught.getLocalizedMessage());
			}
		};
		mDataService.getDelayedActivitesForRole(pNewRole, lDelayedActivityCallback);
	}

	/**
	 * Get current Activity
	 */
	private String getCurrentActivity() {
		return mCurrentActivityLabel.getText();
	}

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		// Initialization of service
		mDataService = (GwtRemoteServiceAsync) GWT.create(GwtRemoteService.class);
		ServiceDefTarget lEndpoint = (ServiceDefTarget) mDataService;
		lEndpoint.setServiceEntryPoint(GWT.getModuleBaseURL() + "/getdata");

		// Initialization of the connection
		AsyncCallback lConnectionCallback = new AsyncCallback() {
			public void onSuccess(Object pResult) {
				// Initialization of mRoleListBox
				AsyncCallback lRoleCallback = new AsyncCallback() {
					public void onSuccess(Object pResult) {
						// Insert roles
						String[] lRoles = (String[]) pResult;
						for (int lI = 0, lMaxI = lRoles.length; lI < lMaxI; lI++) {
							mRoleListBox.addItem(lRoles[lI]);
						}

						String lSelectedRole = mRoleListBox.getItemText(mRoleListBox.getSelectedIndex());

						refreshActivityListBox(lSelectedRole);

						refreshDelayedActivityListBox(lSelectedRole);
					}

					public void onFailure(Throwable pCaught) {
						Window
								.alert(mConstants.asynchronousCommunicationError() + "\n"
										+ pCaught.getLocalizedMessage());
					}
				};
				mDataService.getRoles(lRoleCallback);
			}

			public void onFailure(Throwable pCaught) {
				Window.alert(mConstants.asynchronousCommunicationError() + "\n" + pCaught.getLocalizedMessage());
			}
		};
		mDataService.initConnection(lConnectionCallback);

		// Menu
		mMenu = new HorizontalPanel();
		mMenu.setWidth("100%");
		mMenu.setStyleName("mMenu");

		FlowPanel mControlPanel = new FlowPanel();
		mControlPanel.setWidth("100%");
		mControlPanel.setStyleName("mControlPanel");

		mSidebarToogleButton = new Button(mConstants.hideSidebar());
		mSidebarToogleButton.setPixelSize(150, 20);
		mSidebarToogleButton.addStyleName("mSidebarToogleButton");
		mControlPanel.add(mSidebarToogleButton);
		mSidebarToogleButton.addClickListener(new SidebarListener());

		mNextButton = new Button();
		mNextButton.setPixelSize(24, 24);
		mNextButton.addStyleName("mNextButton");
		mNextButton.setEnabled(false);
		mControlPanel.add(mNextButton);
		mNextButton.addClickListener(new NextButtonListener());

		mDelayButton = new Button();
		mDelayButton.setPixelSize(24, 24);
		mDelayButton.addStyleName("mPauseButton");
		mDelayButton.setEnabled(false);
		mControlPanel.add(mDelayButton);
		mDelayButton.addClickListener(new DelayButtonListener());

		mCancelButton = new Button();
		mCancelButton.setPixelSize(24, 24);
		mCancelButton.addStyleName("mCancelButton");
		mCancelButton.setEnabled(false);
		mControlPanel.add(mCancelButton);
		mCancelButton.addClickListener(new CancelButtonListener());

		mMenu.add(mControlPanel);

		mStatusPanel = new HorizontalPanel();
		mStatusPanel.setWidth("100%");
		mStatusPanel.setStyleName("mStatusPanel");

		// mCurrentActivityPanel
		mCurrentActivityPanel = new HorizontalPanel();
		mCurrentActivityPanel.addStyleName("mCurrentActivityPanel");

		mCurrentActivityLabel = new Label(mConstants.currentActivity());
		mCurrentActivityLabel.addStyleName("mCurrentActivityLabel");
		mCurrentActivityPanel.add(mCurrentActivityLabel);

		mCurrentActivity = new Label();
		mCurrentActivity.addStyleName("mCurrentActivity");
		mCurrentActivityPanel.add(mCurrentActivity);

		mStatusPanel.add(mCurrentActivityPanel);

		// mCurrentActionPanel
		mCurrentActionPanel = new HorizontalPanel();
		mCurrentActionPanel.addStyleName("mCurrentActionPanel");

		mCurrentActionLabel = new Label(mConstants.currentAction());
		mCurrentActionLabel.addStyleName("mCurrentActionLabel");
		mCurrentActionPanel.add(mCurrentActionLabel);

		mCurrentAction = new Label();
		mCurrentAction.addStyleName("mCurrentAction");
		mCurrentActionPanel.add(mCurrentAction);

		mStatusPanel.add(mCurrentActionPanel);

		// mCurrentRolePanel
		mCurrentRolePanel = new HorizontalPanel();
		mCurrentRolePanel.addStyleName("mCurrentRolePanel");

		mCurrentRoleLabel = new Label(mConstants.currentRole());
		mCurrentRoleLabel.addStyleName("mCurrentRoleLabel");
		mCurrentRolePanel.add(mCurrentRoleLabel);

		mCurrentRole = new Label();
		mCurrentRole.addStyleName("mCurrentRole");
		mCurrentRolePanel.add(mCurrentRole);

		mStatusPanel.add(mCurrentRolePanel);

		mMenu.add(mStatusPanel);

		mIconPanel = new HorizontalPanel();
		mIconPanel.setWidth("100%");
		mIconPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
		mIconPanel.setStyleName("mIconPanel");

		mLogo = new Image();
		mLogo.setUrl("AgilClientWeb.png");
		mLogo.setStyleName("mLogo");
		mIconPanel.add(mLogo);

		mMenu.add(mIconPanel);

		mMenu.setCellWidth(mControlPanel, "30%");
		mMenu.setCellWidth(mStatusPanel, "60%");
		mMenu.setCellWidth(mIconPanel, "10%");

		// Content
		mContent = new HorizontalPanel();
		mContent.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);
		mContent.setWidth("100%");
		mContent.setHeight("100%");
		mContent.setStyleName("mContent");

		// Sidebar
		mSidebar = new VerticalPanel();
		mSidebar.setHeight("20%");
		mSidebar.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
		mSidebar.setStyleName("mSidebar");

		mRolePanel = new VerticalPanel();
		mRolePanel.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
		mRolePanel.setStyleName("mRolePanel");

		mRoleLabel = new Label(mConstants.role());
		mRoleLabel.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);
		mRoleLabel.setStyleName("mRoleLabel");
		mRolePanel.add(mRoleLabel);

		mRoleListBox = new ListBox();
		mRoleListBox.setVisibleItemCount(1);
		mRoleListBox.setPixelSize(200, 15);
		mRoleListBox.setStyleName("mRoleListBox");
		mRolePanel.add(mRoleListBox);
		mRoleListBox.addChangeListener(new RoleListBoxListener());

		mSidebar.add(mRolePanel);

		mActivityPanel = new VerticalPanel();
		mActivityPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
		mActivityPanel.setStyleName("mActivityPanel");

		mActivityLabel = new Label(mConstants.activity());
		mActivityLabel.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);
		mActivityLabel.setStyleName("mActivityLabel");
		mActivityPanel.add(mActivityLabel);

		mActivityListBox = new ListBox();
		mActivityListBox.setVisibleItemCount(5);
		mActivityListBox.setPixelSize(200, 75);
		mActivityListBox.setStyleName("mActivityListBox");
		mActivityPanel.add(mActivityListBox);
		mSidebar.add(mActivityPanel);
		mActivityListBox.addChangeListener(new ActivityListBoxListener());

		mDelayedActivityPanel = new VerticalPanel();
		mDelayedActivityPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
		mDelayedActivityPanel.setStyleName("mDeferredActivityPanel");

		mDelayedActivityLabel = new Label(mConstants.delayedActivity());
		mDelayedActivityLabel.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);
		mDelayedActivityLabel.setStyleName("mDelayedActivityLabel");
		mDelayedActivityPanel.add(mDelayedActivityLabel);

		mDelayedActivityListBox = new ListBox();
		mDelayedActivityListBox.setVisibleItemCount(5);
		mDelayedActivityListBox.setPixelSize(200, 75);
		mDelayedActivityListBox.setStyleName("mDelayedActivityListBox");
		mDelayedActivityPanel.add(mDelayedActivityListBox);
		mSidebar.add(mDelayedActivityPanel);
		mDelayedActivityListBox.addChangeListener(new DelayedActivityListBoxListener());

		mParameterPanel = new VerticalPanel();
		mParameterPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
		mParameterPanel.setStyleName("mParameterPanel");
		mParameterPanel.setVisible(false);

		mParameterLabel = new Label(mConstants.parameter());
		mParameterLabel.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);
		mParameterLabel.setStyleName("mParameterLabel");
		mParameterPanel.add(mParameterLabel);

		mGenericParameterPanel = new GwtGenericParameterPanel();
		mGenericParameterPanel.setPixelSize(175, 75);
		mParameterPanel.add(mGenericParameterPanel);

		mSidebar.add(mParameterPanel);

		mContent.add(mSidebar);

		// Frame
		mFrame = new Frame();
		mFrame.setWidth("100%");
		mFrame.setHeight("100%");
		mFrame.setVisible(false);
		mFrame.setStyleName("mFrame");
		mContent.add(mFrame);

		mContent.setCellWidth(mFrame, "100%");
		mContent.setCellHeight(mFrame, "100%");

		// GenericUI
		mGenericUI = new GwtGenericUI();
		mGenericUI.setWidth("100%");
		mGenericUI.setHeight("100%");
		mGenericUI.setVisible(false);
		mContent.add(mGenericUI);

		mContent.setCellWidth(mGenericUI, "100%");
		mContent.setCellHeight(mGenericUI, "100%");

		RootPanel.get("agilwebclient").add(mMenu);
		RootPanel.get("agilwebclient").add(mContent);
	}
}
